package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.DtsPermissionDto;
import com.qf.dto.DtsSerachDto;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zhaojian
 */
@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private DtsRoleMapper roleMapper;
    @Autowired
    private DtsPermissionMapper dtsPermissionMapper;

    private static final Logger logger = LoggerFactory.getLogger(RoleService.class);

    @Override
    public Set<String> queryRolesByIds(Integer[] ids) {
        Set<String> roleNameSet = new HashSet<>();
        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        List<DtsRole> list = roleMapper.selectList(queryWrapper);

        if (list != null) {
            for (DtsRole dtsRole : list) {
                roleNameSet.add(dtsRole.getName());
            }
        }
        return roleNameSet;
    }

    @Override
    public List<DtsRole> findAllRoleList() {
        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        List<DtsRole> list = roleMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Map queryRolesByPage(DtsSerachDto serachDto) {
        //启动分页插件
        PageHelper.startPage(serachDto.getPage(),serachDto.getLimit());
        List<DtsRole> roles = roleMapper.selectRoles(serachDto);
        //pageInfo封装
        PageInfo<DtsRole> pageInfo = new PageInfo<>(roles);
        //封装结果返回数
        Map data = new HashMap();
        data.put("total",pageInfo.getTotal());
        data.put("items",pageInfo.getList());
        return data;
    }

    @Override
    public boolean checkRoleName(String roleName) {
        Long count = roleMapper.queryCountByRoleName(roleName);
        logger.debug("[新增角色]当前系统下存在角色为:{}",roleName,count);
        return count >=1;
    }

    @Override
    public void addRole(DtsRole dtsRole) {
        dtsRole.setAddTime(new Date());
        dtsRole.setUpdateTime(new Date());
        roleMapper.insert(dtsRole);
    }

    @Override
    public int removeRole(Integer id) {
        int count = roleMapper.deleteById(id);
        return count;
    }

    @Override
    public int updateRole(DtsRole dtsRole) {
        //补全数据
        dtsRole.setUpdateTime(new Date());
        int count = roleMapper.updateById(dtsRole);
        return count;
    }

    @Override
    public boolean checkSuper(int roleId) {
        Long count = roleMapper.querySuper(roleId);
        return count >= 1;
    }

    @Override
    public Set<String> queryPermissions(int roleId) {
        Set<String> permissions = roleMapper.queryPermissions(roleId);
        return permissions;
    }

    @Override
    public void removePermisssion(int roleId) {
        dtsPermissionMapper.deleteById(roleId);
    }

    //新增角色
    @Override
    public void savePermission(DtsPermissionDto dtsPermissionDto) {
        for (String permission : dtsPermissionDto.getPermissions()) {
            DtsPermission dtsPermission = new DtsPermission();
            dtsPermission.setPermission(permission);
            dtsPermission.setRoleId(dtsPermissionDto.getRoleId());
            dtsPermission.setDeleted(false);
            dtsPermission.setAddTime(new Date());
            dtsPermissionMapper.insert(dtsPermission);
        }

    }

}
